package com.ahsjdy.st.stservice.service.impl;

import com.ahsjdy.st.stdao.entity.Area;
import com.ahsjdy.st.stdao.mapper.AreaMapper;
import com.ahsjdy.stcommon.dto.AreaDTO;
import com.ahsjdy.stcommon.dto.PageDTO;
import com.ahsjdy.stcommon.exception.BizException;
import com.ahsjdy.stcommon.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author xuetao wang
 * @Date 2020/1/31
 */
@Service
public class AdminCommonServiceImpl {

    @Autowired
    private AreaMapper areaMapper;

    //添加区域信息
    public void addArea(String areaName, String lat, String lon) {
        Area area = new Area();
        area.setModifyTime(new Date());
        area.setStatus(0);//筹备中
        area.setCreateTime(area.getModifyTime());
        area.setLon(lon);
        area.setLat(lat);
        area.setAreaName(areaName);
        areaMapper.insert(area);
    }

    public PageDTO listArea(String areaName) {
        List<Area> areas = areaMapper.listArea(StringUtils.isBlank(areaName) ? null : areaName);
        List<AreaDTO> areaDTOS = new ArrayList<>(areas.size());
        for (Area area : areas) {
            AreaDTO areaDTO = new AreaDTO();
            areaDTOS.add(areaDTO);

            areaDTO.setAreaName(area.getAreaName());
            areaDTO.setCreateTime(DateUtils.dateToString(area.getCreateTime(), "yyyy-MM-dd"));
            areaDTO.setId(area.getId());
            areaDTO.setLat(area.getLat());
            areaDTO.setLon(area.getLon());
            areaDTO.setStatus(area.getStatus());
        }
        return PageDTO.build(areaDTOS, 1, 1);
    }


    @Transactional(rollbackFor = Exception.class)
    public void changeStatusArea(int areaId, int newSatus) {
        Area area = areaMapper.selectByPrimaryKey(areaId);
        if (null == area) {
            throw new BizException("区域不存在");
        }

        if (area.getStatus() == newSatus) {
            return;
        }

        int rows = areaMapper.updateStatus(areaId, area.getStatus(), newSatus);
        if (1 != rows) {
            throw new BizException("操作失败");
        }
    }

    public AreaDTO queryArea(int areaId) {
        Area area = areaMapper.selectByPrimaryKey(areaId);
        AreaDTO areaDTO = new AreaDTO();

        areaDTO.setAreaName(area.getAreaName());
        areaDTO.setCreateTime(DateUtils.dateToString(area.getCreateTime(), "yyyy-MM-dd"));
        areaDTO.setId(area.getId());
        areaDTO.setLat(area.getLat());
        areaDTO.setLon(area.getLon());
        areaDTO.setStatus(area.getStatus());

        return areaDTO;
    }

    public void modifyArea(int areaId, String areaName, String lat, String lon) {
        Area area = areaMapper.selectByPrimaryKey(areaId);
        area.setAreaName(areaName);
        area.setLat(lat);
        area.setLon(lon);

        areaMapper.updateByPrimaryKey(area);
    }
}
